/**
 * Copyright (c) 2024,  科泰集团教研团队
 * 版权所有，请勿侵权
 */
package com.ktjiaoyu.news.service.impl;

import com.ktjiaoyu.news.dao.NewsTypeDao;
import com.ktjiaoyu.news.dao.impl.NewsTypeDaoImpl;
import com.ktjiaoyu.news.entity.NewsType;
import com.ktjiaoyu.news.mapper.NewsTypeMapper;
import com.ktjiaoyu.news.service.NewsTypeService;
import com.ktjiaoyu.news.util.MyBatisUtil;
import com.ktjiaoyu.news.utils.Page;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 新闻分类表 Service接口实现
 * @author 科泰集团教研团队
 * @createTime 2024-4-17 20:49:54
 * @lastModify 2024-4-17 20:49:54
 * @version 1.0.0
 */
public class NewsTypeServiceImpl implements NewsTypeService {

    @Override
    public Page<NewsType> findPage(NewsType entity, int pageNo, int pageSize) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        Page<NewsType> page = new Page<>();
        // 获得总数量
        int count = sqlSession.getMapper(NewsTypeMapper.class).count(entity);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        // 设置总数量，计算总页数
        page.setTotalCount(count);
        List<NewsType> list =  sqlSession.getMapper(NewsTypeMapper.class).getPage(entity, (pageNo-1)*pageSize, pageSize);
        MyBatisUtil.closeSqlSession(sqlSession);
        page.setData(list);

        return page;
    }

    @Override
    public List<NewsType> findAll() {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        List<NewsType> list =  sqlSession.getMapper(NewsTypeMapper.class).getAll();
        MyBatisUtil.closeSqlSession(sqlSession);
        return list;
    }

    @Override
    public NewsType findById(Long id) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        NewsType byId = sqlSession.getMapper(NewsTypeMapper.class).getById(id);
        MyBatisUtil.closeSqlSession(sqlSession);
        return byId;
    }

    @Override
    public boolean saveOrUpdate(NewsType entity) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        boolean flag=false;
        if(entity.getId() != null){
            // 如果有ID就是修改
            flag= sqlSession.getMapper(NewsTypeMapper.class).update(entity)>0;
        }
        // 没有id就是新增
        flag= sqlSession.getMapper(NewsTypeMapper.class).insert(entity)>0;
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return flag;
    }

    @Override
    public boolean importBatch(NewsType... entities) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        boolean flag=false;
        flag= sqlSession.getMapper(NewsTypeMapper.class).insertBatch(entities)>0;
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return flag;
    }


    @Override
    public boolean removeById(Long id) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        boolean flag=false;
        flag= sqlSession.getMapper(NewsTypeMapper.class).deleteById(id)>0;
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return flag;
    }

    @Override
    public boolean removeByEntity(NewsType entity) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        boolean flag=false;
        flag=sqlSession.getMapper(NewsTypeMapper.class).deleteByEntity(entity)>0;
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return flag;
    }

    @Override
    public boolean removeBatch(Long... ids) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        boolean flag=false;
        flag= sqlSession.getMapper(NewsTypeMapper.class).deleteBatch(ids)>0;
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return flag;
    }
}
